package com.example.aiodemo.test;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class CS {
    public static void main(String[] args) {
//        List<String> list = new ArrayList<>();
        Queue<String> queue=new ArrayBlockingQueue<String>(10);
        AtomicInteger i= new AtomicInteger();
        int j=0;
        Runnable r1 = () -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                list.add("s");
//                System.out.println("生产者长度: "+list.size());
                queue.add("商品"+i);
                System.out.println("生产商品"+i);
                i.getAndIncrement();
                System.out.println(j);
            }
        };
        Runnable r2 = () -> {
            while (true) {
                /*if (list.size() > 0){
                    list.remove(0);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("生产者消费");
                }
                else {
                    System.out.println("等待生产者生产....");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }*/
                if (!queue.isEmpty()){
                    String poll = queue.poll();
                    System.out.println("poll "+poll);
                }
                try {
                    Thread.sleep(2500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        };

        Thread t1=new Thread(r1,"生产者");
        Thread t2=new Thread(r2,"消费者");
        t1.start();
        t2.start();
    }
}
@FunctionalInterface
interface A{
    void a();
    default void b(){
        System.out.println(1);
    }
}